<?php
class Product extends Controller
{
    //Điện thoại mới nhất
    public function actionNewestMobile()
    {
        $parentId = 1;
        if(isset($_GET['view']))
        {
           $page = 1;
           $perPage = 14;
            $layout = 'layout/fullwidth';
            $defaultStyle = $this->_loader->config('default_style');

            $this->loadChildControllers(array(
                // left siderbar
                'quick_search' => 'module/QuickSearch',
                'category'      => 'module/Category',
                'contact_info'  => 'module/ContactInfo',
                'support'       => 'module/Support',

                // right sidebar
                'featured_product' => 'module/FeaturedProduct',
                'discounting_product' => 'module/DiscountingProduct',
            ));
        }
        else
        {
            $page = 2;
            $perPage = 3;
            $layout = '';
        }
        $productModel = $this->_loader->model('ProductModel');
        $fieldOrder = 'date_created';
        $fetchOptions = array(
            'page' => $page,
            'perPage' => $perPage
        );
        $viewParams = array(
            'products' => $productModel->getGroupProduct($parentId, $fieldOrder, $fetchOptions),
            'title' => 'Điện thoại mới'
        );
        return $this->responseView('module/newest_product_mobile', $viewParams, $layout);
    }

    //Phụ kiện mới nhất
    public function actionNewestDevice()
    {
        $parentId = 2;
        if(isset($_GET['view']))
        {
            $page = 1;
            $perPage = 14;
            $layout = 'layout/fullwidth';
            $defaultStyle = $this->_loader->config('default_style');

            $this->loadChildControllers(array(
                // left siderbar
                'quick_search' => 'module/QuickSearch',
                'category'      => 'module/Category',
                'contact_info'  => 'module/ContactInfo',
                'support'       => 'module/Support',

                // right sidebar
                'featured_product' => 'module/FeaturedProduct',
                'discounting_product' => 'module/DiscountingProduct',
            ));
        }
        else
        {
            $page = 2;
            $perPage = 3;
            $layout = '';
        }
        $productModel = $this->_loader->model('ProductModel');
        $fieldOrder = 'date_created';
        $fetchOptions = array(
            'page' => $page,
            'perPage' => $perPage
        );
        $viewParams = array(
            'products' => $productModel->getGroupProduct($parentId, $fieldOrder, $fetchOptions),
            'title' => 'Phụ kiện mới'
        );
        return $this->responseView('module/newest_product_device', $viewParams, $layout);
    }

    // Điện thoại bán chạy nhất
    public function actionBestSoldMobile()
    {
        $parentId = 1;
        if(isset($_GET['view']))
        {
            $page = 1;
            $perPage = 14;
            $layout = 'layout/fullwidth';
            $defaultStyle = $this->_loader->config('default_style');
            $this->loadChildControllers(array(
                // left siderbar
                'quick_search' => 'module/QuickSearch',
                'category'      => 'module/Category',
                'contact_info'  => 'module/ContactInfo',
                'support'       => 'module/Support',

                // right sidebar
                'featured_product' => 'module/FeaturedProduct',
                'discounting_product' => 'module/DiscountingProduct',
            ));
        }
        else
        {
            $page = 2;
            $perPage = 3;
            $layout = '';
        }
        $productModel = $this->_loader->model('ProductModel');
        $fieldOrder = 'total_saled';
        $fetchOptions = array(
            'page' => $page,
            'perPage' => $perPage
        );
        $viewParams = array(
            'products' => $productModel->getGroupProduct($parentId, $fieldOrder, $fetchOptions),
            'title' => 'Điện thoại bán chạy'
        );
        return $this->responseView('module/best_sold_product_mobile', $viewParams, $layout);
    }

    //Phụ kiện bán chạy nhất
    public function actionBestSoldDevice()
    {
        $parentId = 2;
        if(isset($_GET['view']))
        {
            $page = 1;
            $perPage = 14;
            $layout = 'layout/fullwidth';
            $defaultStyle = $this->_loader->config('default_style');
            $this->loadChildControllers(array(
                // left siderbar
                'quick_search' => 'module/QuickSearch',
                'category'      => 'module/Category',
                'contact_info'  => 'module/ContactInfo',
                'support'       => 'module/Support',

                // right sidebar
                'featured_product' => 'module/FeaturedProduct',
                'discounting_product' => 'module/DiscountingProduct',
            ));
        }
        else
        {
            $page = 2;
            $perPage = 3;
            $layout = '';
        }
        $productModel = $this->_loader->model('ProductModel');
        $fieldOrder = 'total_saled';
        $fetchOptions = array(
            'page' => $page,
            'perPage' => $perPage
        );
        $viewParams = array(
            'products' => $productModel->getGroupProduct($parentId, $fieldOrder, $fetchOptions),
            'page' => $page,
            'perPage' => $perPage,
            'title' => 'Phụ kiện bán chạy'
        );
        return $this->responseView('module/best_sold_product_device', $viewParams, $layout);
    }

    //Chi tiết sản phẩm
    public function actionDetail($productId)
    {
        self::$crossParams['title'] = 'THÔNG SỐ VÀ MÔ TẢ SẢN PHẨM';

        if(isset($productId))
        {
            $defaultStyle = $this->_loader->config('default_style');

            $this->loadChildControllers(array(
                // left siderbar
                'quick_search' => 'module/QuickSearch',
                'category'      => 'module/Category',
                'contact_info'  => 'module/ContactInfo',
                'support'       => 'module/Support',
                'header'       => 'module/header',
                'footer'       => 'module/footer',

            ));

            $fetchOptions = array(
                'page' => 1,
                'perPage' => 15
            );
            $productModel = $this->_loader->model('ProductModel');

            $viewParams = array(
                'product' => $productModel->getProductById($productId),
                'product_attributes' => $productModel->getProductAttribute($productId, $fetchOptions),
                'product_comments' => $productModel->getProductComment($productId, $fetchOptions),
            );

            return $this->responseView('module/detail_product', $viewParams, 'layout/left_sidebar');
        }
        return $this->responseView('module/detail_product', array());
    }

    public function actionList()
    {
        $this->_loader->helper('Html');

        $page = $this->_input->filterSingle('page', Input::UINT);
        $page = $page == 0? 1 : $page;
        $perPage = 15;

        $this->setTitle('Danh sách sản phẩm');
        $fieldOrder = 'price';
        $fetchOptions = array(
            'page' => $page,
            'perPage' => $perPage
        );
        $productModel = $this->_loader->model('ProductModel');
        $viewParams = array(
            'products' => $productModel->getProductsByParam($fieldOrder, $fetchOptions),
            'page' => $page,
            'perPage' => $perPage

        );
        $defaultStyle = $this->_loader->config('default_style');
        $this->loadChildControllers(array(
            // left siderbar
            'quick_search' => 'module/QuickSearch',
            'category'      => 'module/Category',
            'contact_info'  => 'module/ContactInfo',
            'support'       => 'module/Support',
            'header'       => 'module/header',
            'footer'       => 'module/footer',

        ));

        return $this->responseView('module/list_product', $viewParams, 'layout/left_sidebar');
    }

    public function actionSearchProduct()
    {
        $this->_loader->helper('Html');

        $page = ($page = $this->_input->filterSingle('page', Input::UINT)) < 1 ? 1 : $page;
        $perPage = 10;
        $this->setTitle('Kết quả tìm kiếm');

        $conditions = array(
            'product_name' => $_POST['key_search'],
            'from_price' => $_POST['from_price'],
            'to_price' => $_POST['to_price']
        );
        $fetchOptions = array(
            'page' => $page,
            'perPage' => $perPage
        );

        $productModel = $this->_loader->model('ProductModel');
        $viewParams = array(
            'products' => $productModel->getProducts($conditions, $fetchOptions),
            'page' => $page,
            'perPage' => $perPage
        );

        //Load interface
        $defaultStyle = $this->_loader->config('default_style');
        $this->loadChildControllers(array(
            // left siderbar
            'quick_search' => 'module/QuickSearch',
            'category'      => 'module/Category',
            'contact_info'  => 'module/ContactInfo',
            'support'       => 'module/Support',
            'header'       => 'module/header',
            'footer'       => 'module/footer',
        ));

        return $this->responseView('module/list_product', $viewParams, 'layout/left_sidebar');
    }

    public function actionByCategory($productCategoryId)
    {
        $this->_loader->helper('Html');
        $page = ($page = $this->_input->filterSingle('page', Input::UINT)) < 1 ? 1 : $page;
        $perPage = 10;
        $this->setTitle('Sản phẩm theo loại');
        $fieldOrder = 'price';
        $fetchOptions = array(
            'page' => $page,
            'perPage' => $perPage
        );
        $productModel = $this->_loader->model('ProductModel');
        $viewParams = array(
            'products' => $productModel->getProductByCategory($productCategoryId, $fieldOrder, $fetchOptions),
            'page' => $page,
            'perPage' => $perPage     ,
            'category' => array('category_id' => $productCategoryId)
        );

        //Load interface
        $defaultStyle = $this->_loader->config('default_style');
        $this->loadChildControllers(array(
            // left siderbar
            'quick_search' => 'module/QuickSearch',
            'category'      => 'module/Category',
            'contact_info'  => 'module/ContactInfo',
            'support'       => 'module/Support',
            'header'       => 'module/header',
            'footer'       => 'module/footer',
        ));

        return $this->responseView('module/product_category', $viewParams, 'layout/left_sidebar');
    }

    public function actionAddComment($productId)
    {
        $this->_loader->helper('Html');
        $customerId = $this->_session->get('customerId');
        $content = $this->_input->filterSingle('content_comment', Input::STRING);
        $date = time();
        $productModel = $this->_loader->model('ProductModel');
        if($customerId)
        {
            $productModel->getAddComment($productId, $customerId, $content, $date);
        }

       return  $this->responseRedirect('product/detail/' . $productId);
    }
}